package excelexporter;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;

import org.apache.log4j.Logger;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import db.DatabaseManager;

import util.SimpleFileFilter;

public class ExcelExporter {
	
	private static final Logger log = Logger.getLogger(DatabaseManager.class);
	
	public String getCanonicalPath(String filename)
	{
		/*
		final JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(new java.io.File("."));
		chooser.setDialogTitle("Please Select Output Directory");
		chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		chooser.setAcceptAllFileFilterUsed(false);
		
		if (!filename.endsWith(".xls")) {
			filename = filename + ".xls";
    	}
		chooser.setSelectedFile(new File(filename));
		    
		if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
			try {
				return chooser.getSelectedFile().getCanonicalPath();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
		*/
		
		JFileChooser fc = new JFileChooser();
    	fc.setFileSelectionMode(0);
    	fc.setFileFilter(new SimpleFileFilter("xls"));
    	
    	int returnVal = fc.showSaveDialog(null);
    	if (returnVal == 0) {
    		File file = fc.getSelectedFile();
    		
    		String path = file.getAbsolutePath();
    		
    		if (!path.endsWith(".xls")) {
    		path = path + ".xls";
    		}
    		
    		return path;
    	}
    	
    	return null;
	}
	
	public void generateQueryDataExcel(List<QueryDataBean> stationList)
	{
		Map<String, List<QueryDataBean>> beans = new HashMap<String, List<QueryDataBean>>();
        beans.put("station", stationList);
        
        XLSTransformer transformer = new XLSTransformer();
      
        String path = getCanonicalPath("StationList.xls") ;
        if(path != null)
        {
        	try {
    			transformer.transformXLS("./resources/StationTemplate.xls", beans,  path);
    			JOptionPane.showMessageDialog(null, "Data successfully exported to Excel.", "Export Successfully", JOptionPane.PLAIN_MESSAGE);
        	} catch (ParsePropertyException e) {
    			log.error("generateQueryDataExcel",e);
    			e.printStackTrace();
    		} catch (InvalidFormatException e) {
    			log.error("generateQueryDataExcel",e);
    			e.printStackTrace();
    		} catch (IOException e) {
    			log.error("generateQueryDataExcel",e);
    			e.printStackTrace();
    		}
        }
    	
	}
	
	public void generateSPCExcelData(List<QuerySPCBean> padList)
	{
		Map<String, List<QuerySPCBean>> beans = new HashMap<String, List<QuerySPCBean>>();
        beans.put("padList", padList);
        
        XLSTransformer transformer = new XLSTransformer();
      
        String path = getCanonicalPath("SPC.xls") ;
        if(path != null)
        {
        	try {
    			transformer.transformXLS("./resources/SPCTemplate.xls", beans,  path);
    			JOptionPane.showMessageDialog(null, "Data successfully exported to Excel.", "Export Successfully", JOptionPane.PLAIN_MESSAGE);
        	} catch (ParsePropertyException e) {
    			log.error("generateSPCExcelData",e);
    			e.printStackTrace();
    		} catch (InvalidFormatException e) {
    			log.error("generateSPCExcelData",e);
    			e.printStackTrace();
    		} catch (IOException e) {
    			log.error("generateSPCExcelData",e);
    			e.printStackTrace();
    		}
        }
    	
	}
	
	public void generateTendChartBean(List<TendChartBean> tendChartList)
	{
	
		Map<String, List<TendChartBean>> beans = new HashMap<String, List<TendChartBean>>();
        beans.put("tendChartList",tendChartList);
        
        XLSTransformer transformer = new XLSTransformer();
      
        String path = getCanonicalPath("Tend.xls") ;
        if(path != null)
        {
        	try {
    			transformer.transformXLS("./resources/TaperTemplate.xls", beans,  path);
    			JOptionPane.showMessageDialog(null, "Data successfully exported to Excel.", "Export Successfully", JOptionPane.PLAIN_MESSAGE);
        	} catch (ParsePropertyException e) {
    			log.error("generateTendChartBean",e);
    			e.printStackTrace();
    		} catch (InvalidFormatException e) {
    			log.error("generateTendChartBean",e);
    			e.printStackTrace();
    		} catch (IOException e) {
    			log.error("generateTendChartBean",e);
    			e.printStackTrace();
    		}
        }
    	
	}
	
	public void generateParetoExcelData(List<ParetoChartBean> paretoChartList)
	{
	
		Map<String, List<ParetoChartBean>> beans = new HashMap<String, List<ParetoChartBean>>();
        beans.put("paretoChartList",paretoChartList);
        
        XLSTransformer transformer = new XLSTransformer();
      
        String path = getCanonicalPath("Pareto.xls") ;
        if(path != null)
        {
        	try {
    			transformer.transformXLS("./resources/ParetoTemplate.xls", beans,  path);
    			JOptionPane.showMessageDialog(null, "Data successfully exported to Excel.", "Export Successfully", JOptionPane.PLAIN_MESSAGE);
        	} catch (ParsePropertyException e) {
    			log.error("generateParetoExcelData",e);
    			e.printStackTrace();
    		} catch (InvalidFormatException e) {
    			log.error("generateParetoExcelData",e);
    			e.printStackTrace();
    		} catch (IOException e) {
    			log.error("generateParetoExcelData",e);
    			e.printStackTrace();
    		}
        }
    	
	}
	
	public void generateActivityLog(List<ActivityBean> activityList)
	{
	
		Map<String, List<ActivityBean>> beans = new HashMap<String, List<ActivityBean>>();
        beans.put("activityList",activityList);
        
        XLSTransformer transformer = new XLSTransformer();
      
        String path = getCanonicalPath("Activity.xls") ;
        if(path != null)
        {
        	try {
    			transformer.transformXLS("./resources/ActivityTemplate.xls", beans,  path);
    			JOptionPane.showMessageDialog(null, "Data successfully exported to Excel.", "Export Successfully", JOptionPane.PLAIN_MESSAGE);
        	} catch (ParsePropertyException e) {
    			log.error("generateActivityLog",e);
    			e.printStackTrace();
    		} catch (InvalidFormatException e) {
    			log.error("generateActivityLog",e);
    			e.printStackTrace();
    		} catch (IOException e) {
    			log.error("generateActivityLog",e);
    			e.printStackTrace();
    		}
        }
    	
	}
}
